<?php

namespace structure\components;

class Link extends Text
{
	private $params = array("class", "id", "style");
	private $page;
	private $title;
	private $ruleSet;

	public function Start()
	{
		//parent::Start();
		$this->SetPage($this->GetComponent()->GetAttribute("page"));
		$this->SetTitle($this->GetComponent()->GetAttribute("content"));

		//Set links specific rules for each state
		$this->SetRuleSet();

		$link = $this->BuildLink();
		$wrap = $this->BuildWrap();

		//Got valid wrap object
		if (is_object($wrap))
		{
			$wrap->AddChild($link);
			return $wrap;
		}
		else
		{
			return $link;
		}
	}

	protected function SetPage($page)
	{
		$this->page = $page;
	}

	protected function GetPage()
	{
		return $this->page;
	}

	protected function SetTitle($title)
	{
		return $this->title = $title;
	}

	protected function GetTitle()
	{
		return $this->title;
	}

	private function SetRuleSet()
	{
		$suppliedAttrs = $this->GetComponent()->GetAttributes();

		if (!isset($suppliedAttrs['ruleset']))
		{
			return false;
		}

		if (!is_array($this->ruleSet))
		{
			$this->ruleSet = service("db")->
			Select("id_ AS id", "class", "style",
			"currentId", "currentClass", "currentStyle",
			"innerId", "innerClass", "innerStyle",
			"wrap", "wrapId", "wrapClass", "wrapStyle",
			"currentWrap", "currentWrapId", "currentWrapClass", "currentWrapStyle",
			"innerWrap", "innerWrapId", "innerWrapClass", "innerWrapStyle")->
			From("_links")->
			Where("name", "=", "?")->
			Placeholders($this->GetComponent()->GetAttribute("ruleset"))->
			Fetch("row")->
			Run();
		}

		//Should we use the ruleset or supplied attrs?
		if (isset($suppliedAttrs['ruleset']))
		{
			if (!empty($this->ruleSet))
			{
				$suppliedAttrs = $this->ruleSet;
			}
		}

		foreach ($this->ruleSet as $key=>$value)
		{
			$this->GetCoponent()->SetAttribute($key, $value);
		}

		return true;
	}

	private function GetLevel($page = false)
	{
		if (!$page)
		{
			$page = $this->GetPage();
		}

		if (!core("Page")->PageExists($page))
		{
			service("debug")->error("Can't create link to page '" . $page . "' which desn't exist");
		}

		$url = core("Page")->GetUrl(true);

		$level = "";

		if (in_array($page, $url))
		{
			if (end($url) == $page)
			{
				$level = "current";
			}
			else
			{
				$level = "inner";
			}
		}
		else
		{
			$level = "";
		}

		return $level;
	}
	/*
	protected function Build()
	{

	}*/

	protected function BuildLink()
	{
		$level = $this->GetLevel();

		if ($level == "current")
		{
			return $this->GetTitle();
		}

		//Create element
		$el = core("Html")->CreateElement("a", $this->GetTitle());

		$el->AddAttribute("href", "/".core("Page")->GetPageParams($this->GetPage(), "url"));

		//Add current level-specific params
		$pars = $this->BuildLinkAttributes();

		$el->AddAttributes($pars);

		return $el;
	}

	private function BuildLinkAttributes()
	{
		$basicAttrs = $this->params;
		$attrs = $this->GetComponent()->GetAttributes();

		$resultingAttrs = array();

		$level = $this->GetLevel();

		foreach ($basicAttrs as $item)
		{
			$initItem = $item;

			if (!empty($level))
			{
				$item = $level . ucfirst($item);
			}

			if (isset($attrs[$item]))
			{
				$resultingAttrs[$initItem] = $attrs[$item];
			}
		}

		return $resultingAttrs;
	}

	private function BuildWrap()
	{
		$level = $this->GetLevel();

		$wrap = "wrap";

		if ($level)
		{
			$wrap = $level . ucfirst($wrap);
		}

		//If ruleset is set, no wrap attribute will be set
		if (!$this->GetComponent()->AttributeExists($wrap))
		{
			//die("No $wrap<br />");
			return false;
		}

		$wrapTag = $this->GetComponent()->GetAttribute($wrap);

		//Create element
		$el = core("Html")->CreateElement($wrapTag);

		//Add current level-specific params
		$attrs = $this->BuildWrapAttributes();

		$el->AddAttributes($attrs);

		return $el;
	}

	private function BuildWrapAttributes()
	{
		$basicAttrs = $this->params;
		$attrs = $this->GetComponent()->GetAttributes();

		$resultingAttrs = array();

		$level = $this->GetLevel();

		foreach ($basicAttrs as $item)
		{
			$initItem = $item;

			if (!empty($level))
			{
				$item = $level . "Wrap" . ucfirst($item);
			}
			else
			{
				$item = "wrap" . ucfirst($item);
			}

			if (isset($attrs[$item]))
			{
				$resultingAttrs[$initItem] = $attrs[$item];
			}
		}

		return $resultingAttrs;
	}
}